package com.bike.bikeserver.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.bike.bikeserver.common.enums.ModelParamsEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/8/17 20:29
 */
@Slf4j
public class ModelUtils
{
    /**
     * 调用YOLOv5接口，得到单车检测结果
     *
     * @param api
     * @param params
     * @return
     */
    public static String getYoloResult(String api, JSONObject params)
    {
        log.info("请求地址为：" + api);
        ResponseEntity<String> response = HttpUtils.sendPostV2(api, params);
        String responseBody = response.getBody();
        JSONObject jsonObject = JSONObject.parseObject(responseBody);
        String resLogs = jsonObject.getString("logs");
        log.info("请求地址为：" + api + "，返回日志结果为：" + resLogs);
        return resLogs;
    }

    /**
     * 判断是否属于单车淤积
     *
     * @param resLogs
     * @return
     */
    public static boolean judgeIfTooManyBikes(String resLogs)
    {
        String pattern = "\\d+\\s+shared_bike";
        Pattern regexPattern = Pattern.compile(pattern);
        Matcher matcher = regexPattern.matcher(resLogs);
        int count = 0;
        if (matcher.find())
        {
            String matchedText = matcher.group();
            String[] parts = matchedText.split(" ");
            if (parts.length > 1)
            {
                count = Integer.parseInt(parts[0]);
            }
        }
        // 获取最大单车数量
        int most_bike_num = Integer.parseInt(ModelParamsEnums.MOST_BIKE_NUM.getValue());

        return count > most_bike_num;
    }


    /**
     * 调用DeepLabV3接口，得到单车乱停乱放检测结果
     *
     * @param api
     * @param params
     * @return
     */
    public static String getDeepLabV3Result(String api, JSONObject params)
    {
        log.info("请求地址为：" + api);
        ResponseEntity<String> response = HttpUtils.sendPostV2(api, params);
        String responseBody = response.getBody();
        JSONObject jsonObject = JSONObject.parseObject(responseBody);
        String resLogs = jsonObject.getString("msg");
        log.info("请求地址为：" + api + "，返回结果为：" + resLogs);
        return resLogs;
    }

    /**
     * 判断是否属于乱停乱放
     *
     * @param resLogs
     * @return
     */
    public static boolean judgeIfParkingWrongly(String resLogs)
    {
        String patternString = "(\\d+(?:\\.\\d+)?)%";

        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(resLogs);

        if (matcher.find())
        {
            String percentageValue = matcher.group(1);
            System.out.println("Percentage Value: " + percentageValue);
            double parseDouble = Double.parseDouble(percentageValue);
            if (parseDouble >= Double.parseDouble(ModelParamsEnums.PARKING_RIGHTLY_RATE.getValue()))
            {
                return false;
            }
        }

        return true;
    }

    /**
     * 调用综合模型接口，得到yolo和deeplab综合检测结果
     *
     * @param api
     * @param params
     * @return
     */
    public static String getBlendDetectImg(String api, JSONObject params)
    {
        log.info("请求地址为：" + api);
        ResponseEntity<String> response = HttpUtils.sendPostV2(api, params);
        String responseBody = response.getBody();
        JSONObject jsonObject = JSONObject.parseObject(responseBody);
        String imgCode = jsonObject.getString("msg");
        log.info("请求地址为：" + api + "，返回结果为：" + imgCode);
        return imgCode;
    }

    /**
     * 根据正则表达式获取物体数量
     *
     * @param resLogs
     * @param pattern
     * @return
     */
    public static Integer getObjNumByPattern(String resLogs, String pattern)
    {
        Pattern regexPattern = Pattern.compile(pattern);
        Matcher matcher = regexPattern.matcher(resLogs);
        int count = 0;
        if (matcher.find())
        {
            String matchedText = matcher.group();
            String[] parts = matchedText.split(" ");
            if (parts.length > 1)
            {
                count = Integer.parseInt(parts[0]);
            }
        }

        return count;
    }

    /**
     * 调用LSTM接口，得到单车流量预测结果
     *
     * @param api
     * @param params
     * @return
     */
    public static String getLstmResult(String api, JSONObject params)
    {
        log.info("请求地址为：" + api);
        ResponseEntity<String> response = HttpUtils.sendPostV2(api, params);
        String responseBody = response.getBody();
        JSONObject jsonObject = JSONObject.parseObject(responseBody);
        String resLogs = jsonObject.getString("msg");
        log.info("请求地址为：" + api + "，返回结果为：" + resLogs);
        return resLogs;
    }

}
